package demo01;

import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.*;

public class test {

    public static void main(String[] args) throws DocumentException {
        List<Map<String, String>> zhangsan = getStuScore("zhangSan", null);
        for (Map<String, String> stringStringMap : zhangsan) {
            System.out.println(stringStringMap);
        }

    }
    public static List<Map<String,String>> getStuScore(String sname, String cname) throws DocumentException {
       List<Map<String,String>> list = new ArrayList<>();
        List<Course> course = getCourse();
        List<Student> student = getStudent();
        List<score> score = getScore();

        String cc=null;

       if ( cname==null){
           for (Student student1 : student) {
               if (sname.equals(student1.getSname())){
                   cc=student1.getSid();
               }
           }
       }else {

           String aa=null;
           for (Course course1 : course) {
               if (cname.equals(course1.getCid())){
                   aa = course1.getCid();
               }
           }



           String bb=null;
           for (Student student1 : student) {
               if (student1.getSname().equals(sname)){
                   bb=student1.getSid();
               }
           }



           for (demo01.score score1 : score) {
               Map<String,String> map=new HashMap<>();
               if (aa.equals(score1.getCid())&&bb.equals(score1.getSid())){
                   map.put(sname,score1.getScore());
                   list.add(map);
               }
           }
       }


        for (demo01.score score1 : score) {
            Map<String,String> map=new HashMap<>();
            if (cc.equals(score1.getSid())){
                map.put(sname,score1.getScore());
                list.add(map);
            }
        }




        //代码填充






        return list;
    }
    public static List<Student> getStudent() throws DocumentException {



       // 代码填充
        List<Element> elements = new SAXReader().read("student.xml").getRootElement().elements();
        List<Student> list=new ArrayList<>();
        for (Element element : elements) {
            String gender = element.attributeValue("gender");
            String age = element.attributeValue("age");
            String sname = element.attributeValue("sname");
            String sid = element.attributeValue("sid");
           list.add(new Student(gender,age,sname,sid));
        }



        return list;
    }
    public static List<Course> getCourse() throws DocumentException {



        //代码填充
        List<Course> list=new ArrayList<>();
        List<Element> elements = new SAXReader().read("coures.xml").getRootElement().elements();
        for (Element element : elements) {
            String chour = element.attributeValue("chour");
            String cname = element.attributeValue("cname");
            String cid = element.attributeValue("cid");
           list.add(new Course(chour,cname,cid));
        }

        Set<Course> set=new HashSet<>(list);
        list.clear();
        list.addAll(set);


        return list;
   }
    public static List<score> getScore() throws DocumentException {



        //代码填充
        List<Element> elements = new SAXReader().read("score.xml").getRootElement().elements();
        List<score> list=new ArrayList<>();
        for (Element element : elements) {
            String score = element.attributeValue("score");
            String cid = element.attributeValue("cid");
            String sid = element.attributeValue("sid");
       list.add(new score(score,cid,sid));
        }

        return list;    }
}

